package com.ez.supervise.action.sys;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.MyUtils;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.sys.User;
import com.ez.supervise.service.UserService;
import com.ez.supervise.util.JsonUtils;

//import com.lhq.prj.bms.core.MyUtils;

public class UserAction extends BaseJsonAction {

    private static final long serialVersionUID = 967358559670907839L;

    private UserService userService;

    private User user;

    private Page page;

    private Long uerId;

    /**
     * 
     * @return
     */
    public String saveUser() {
	try {
	    if (user.isNew()) {
		userService.insert(user);
	    } else {
		userService.update(user);
	    }
	    Result result = new Result(user.getId());
	    outJsonString(JSONObject.fromObject(result).toString());
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * 
     * @return
     * @throws Exception
     */
    public String findAllUser() throws Exception {
	page = new Page();
	String searchField = getRequest().getParameter("searchField");
	String searchValue = getRequest().getParameter("searchValue");
	if(StringUtils.isNotBlank(searchValue)){
	    page.putCondition(searchField, searchValue.trim());
	} 
	int start = Integer.valueOf(getRequest().getParameter("start"));
	int limit = Integer.valueOf(getRequest().getParameter("limit"));
	page.setStart(++start);
	page.setLimit(limit = limit == 0 ? 20 : limit);
	page.addPageInfo();
	page = userService.findByPage(page);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "createTime", "modifyTime", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	outJsonString(JsonUtils.fromObject(page, new String[] { "createTime", "modifyTime", "new", }));
	return SUCCESS;

    }

    public String findByOrg() throws Exception {
	List result = new ArrayList();
	String strOrgId =  getRequest().getParameter("orgId");
	if(StringUtils.isBlank(strOrgId)){
	    outJsonString("[]");
	    return SUCCESS;
	}
	Long orgId = NumberUtils.toLong(strOrgId);
	result = userService.findByOrg(orgId);
	page = new Page();
	page.setRoot(result);
	page.setTotalProperty(result.size());
	outJsonString(JsonUtils.fromObject(page, new String[] { "creator","createTime", "modifier", "modifyTime", "orgId","email","phone","mobile",
	    "address","zipCode","sex","birthday","duty","isManager","isDemocracy","democracyId"}));
	return SUCCESS;
    }

    /**
     * 
     * @return
     */
    public String deleteUser() {
	try {
	    if (uerId != null && uerId > 0) {
		userService.delete(uerId);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * 
     * @return
     * @throws Exception
     */
    public String updateUser() throws Exception {
	// String fieldName = getRequest().getParameter("fieldName");
	// String strCompanyId = getRequest().getParameter("companyId");
	// String strDeptId = getRequest().getParameter("deptId");
	// String strDutyId = getRequest().getParameter("dutyId");
	// String fieldValue = getRequest().getParameter("fieldValue");
	// String strUserId = getRequest().getParameter("userId");
	// if (strUserId != null && !"".equals(strUserId)) {
	// User user = new User();
	// if ("companyName".equals(fieldName) && !"".equals(strCompanyId)) {
	// user.setCompanyId(Integer.valueOf(strCompanyId));
	// }
	// if ("deptName".equals(fieldName) && !"".equals(strDeptId)) {
	// user.setDeptId(Integer.valueOf(strDeptId));
	// }
	// if ("dutyName".equals(fieldName) && !"".equals(strDutyId)) {
	// user.setDutyId(Integer.valueOf(strDutyId));
	// }
	// user.setUserId(Integer.valueOf(userId));
	// MyUtils.invokeSetMethod(fieldName, user,
	// new Object[] { fieldValue });
	// success = userService.updateUser(user);
	// }
	return SUCCESS;
    }

    public String findAllCategoryName() {
	return SUCCESS;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

    public boolean isSuccess() {
	return success;
    }

    public void setSuccess(boolean success) {
	this.success = success;
    }

    public User getUser() {
	return user;
    }

    public void setUser(User user) {
	this.user = user;
    }

    public void setUserService(UserService userService) {
	this.userService = userService;
    }

    public void setUerId(Long uerId) {
	this.uerId = uerId;
    }

    public Long getUerId() {
	return uerId;
    }

}
